<template>
  <div class="air">
    <el-row :gutter="20">
        <el-col :span="24" class="paddCol" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span v-if="this.alarmstate != 0 && this.alarmstate != 3" style="color:#ea2e2e"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}} (离线)</span></span>
                <span v-if="this.alarmstate == 0 || this.alarmstate == 3"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}}</span></span>
              </div>
              <div class="temp_card">
                <div class="temp_data">
                  <div class="temp_datal">
                    <div class="device_border">
                      <div class="device_top">
                          <div class="device_title">
                            <i class="el-icon-info"></i>
                            设备当前参数
                          </div>
                          <div class="device_datatop">
                            <div class="device_dianya">
                              <img src="../../assets/dy.png" alt="">
                              <div class="device_dianya_data">{{this.data.unit[20].value.toFixed(0)}}</div>
                            </div>
                            <div class="device_dianya_right">
                              <div class="device_data_flex">
                                <img src="../../assets/dla.png" alt="">
                                <div class="device_data_title">电流:</div>
                                <div class="device_data_value">{{this.data.unit[21].value.toFixed(2)}}</div>
                                <div class="device_data_flag">A</div>
                              </div>
                              <div class="device_data_flex">
                                <img src="../../assets/gl.png" alt="">
                                <div class="device_data_title">功率:</div>
                                <div class="device_data_value">{{this.data.unit[22].value.toFixed(0)}}</div>
                                <div class="device_data_flag">W</div>
                              </div>
                              <div class="device_data_flex">
                                <img src="../../assets/dl.png" alt="">
                                <div class="device_data_title">电量:</div>
                                <div class="device_data_value">{{this.data.unit[23].value.toFixed(3)}}</div>
                                <div class="device_data_flag">KWH</div>
                              </div>
                            </div>
                          </div>
                          <div class="device_databottom">
                            <div class="device_databottom_div">
                              <div class="device_databottom_flex">
                                <div class="device_databottom_title">当前温度:</div>
                                <div class="device_databottom_value">{{this.data.unit[5].value.toFixed(1)}}</div>
                                <div class="device_databottom_flag">℃</div>
                              </div>
                              <div class="device_databottom_flex">
                                <div class="device_databottom_title">雷击次数统计:</div>
                                <div class="device_databottom_value1">{{this.data.unit[10].value.toFixed(0)}}</div>
                              </div>
                            </div>
                            <div class="device_databottom_div">
                              <div class="device_databottom_flex">
                                <div class="device_databottom_title1">当前湿度:</div>
                                <div class="device_databottom_value">{{this.data.unit[6].value.toFixed(1)}}</div>
                                <div class="device_databottom_flag">%RH</div>
                              </div>
                              <div class="device_databottom_flex device_databottom_flex2">
                                <div class="device_databottom_title1">空开状态:</div>
                                <div class="device_databottom_value2">
                                  <span v-if="this.data.unit[35].value == 0">断电</span>
                                  <span v-if="this.data.unit[35].value == 1">送电</span>
                                </div>
                              </div>
                            </div>
                          </div>
                      </div>
                    </div>
                    <div class="temp_time_mode">
                      <div class="temp_peak_name">
                        <i class="el-icon-time"></i>
                        时间统计
                      </div>
                      <div class="temp_time">
                        <div class="temp_time_line">
                          <p class="temp_warning_title">首次上线：</p>
                          <p class="temp_time_year">{{ this.firstconnecttimeYear }} </p> 
                          <p class="temp_time_hour">{{ this.firstconnecttimeHour}}</p>
                        </div>
                        <div class="temp_time_line" v-if="this.connecttime > 0">
                          <p class="temp_warning_title">最近上线：</p>
                          <p class="temp_time_year">{{ this.connecttimeYear }} </p>
                          <p class="temp_time_hour">{{ this.connecttimeHour }}</p>
                        </div>
                        <div class="temp_time_line" v-if="this.devtimeshow == 1">
                          <p class="temp_warning_title">累计时长：</p>
                          <p class="temp_time_h"> {{this.totaltime}} </p> <span class="temp_time_s"> h</span>
                        </div>
                      </div>
                    </div> 
                  </div>
                </div>
              </div>
            </el-card>
        </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: ["msg"],
  data() {
    return {
      alarmstate:0,//在线离线
      timeGetData:'',//接口方法的定时器
      data: "", //当前设备数据
      dataor:1,//判断接口是否成功，1表示有返回成功，0表示失败，3之内无返回就置0表示失败
      //控制项
      deng:false,//登录状态
      //属性数据
      firstconnecttimeYear:'',//首次上线年月日
      firstconnecttimeHour:'',//首次上线时分
      connecttime:0,//最近上线时间戳
      connecttimeYear:'',//最近上线年月日
      connecttimeHour:'',//最近上线时分
      totaltime:0,//累计运行时间
      devtimeshow:localStorage.getItem('累计时长'),//累计运行时间显示隐藏
      // 曲线结束---
      localname:'',//本地储存名称
    };
  },
  mounted() {
  },
  created() {
    // 判断是否是直接访问当前设备页面
    if(localStorage.getItem('当前访问路径') == 'overview2'){
      this.localname = "6.0展示设备卡片"
    }else{
      this.localname = "当前设备编号"
    }
    this.data = JSON.parse(localStorage.getItem("menuDataJson"))
    // 曲线内容---
    this.info = JSON.parse(localStorage.getItem(this.localname))
    // 曲线内容---
    this.getData(JSON.parse(localStorage.getItem(this.localname)))
    this.getDataStart()
  },
  methods: {
    //重新定时走接口
    getDataStart(){
      this.$nextTick(() => {
          this.timeGetData = setInterval(() => {
            if(this.dataor == 0){ //接口失败提示
              this.$message({
                message: '服务器访问失败',
                type: 'error',
                duration: 1000
              });
            }
            this.getData(JSON.parse(localStorage.getItem(this.localname)))
            this.dataor = 0
          }, 3000); 
      })
    },
    getData(info){
      // 曲线内容---
      this.info = info
      // 曲线内容---
      if(info.uid == null || info.uid == "null"){//清空缓存第一次进入时找不到info.uid，页面为空页面的处理
        info.uid = 0
      }
      var params = {
          "getdevinfo":[
            {
              "uid": info.uid,
              "type": info.type,
              "addr": info.addr,
              "unittype":"id-name-ready-value-flag"
            }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
        this.dataor = 1// 接口成功置1
        if(res.data.getdevinfo == undefined){ //数据返回不全失败提示
          this.$message({
            message: '服务器访问失败',
            type: 'error',
            duration: 1000
          });
        }else{
          this.data = res.data.getdevinfo[0];
          this.alarmstate = res.data.getdevinfo[0].alarmstate
          // 上次上线时间
          this.firstconnecttimeYear = this.formatDate(this.data.firstconnecttime*1000, 'YYYY-MM-DD')
          this.firstconnecttimeHour = this.formatDate(this.data.firstconnecttime*1000, 'H:m')
          // 最近上线时间
          this.connecttime = this.data.connecttime
          this.connecttimeYear = this.formatDate(this.data.connecttime*1000, 'YYYY-MM-DD')
          this.connecttimeHour = this.formatDate(this.data.connecttime*1000, 'H:m')
          //累计运行时间 拿到的分钟转成小时
          this.devtimeshow = localStorage.getItem('累计时长')
          this.totaltime = (this.data.totaltime/60).toFixed(0)

        }
      })
    },
  },
  // 清除定时器
    beforeDestroy() {
        clearInterval(this.timeGetData);
        this.timeGetData = null;
        // 下发控制计时器
        clearInterval(this.openGetData);
        this.openGetData = null;
    },
};
</script>

<style scoped>
.el-card {
  background: none;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-card__header {
  background: rgba(89, 129, 165, 0.2);
  padding: 16px 20px;
  border-bottom: none;
}
.box-card {
  /* overflow: hidden; */
}
.temp_card{
  height: 850px;
}
.clearfix{
  font-size: 18px;
}
.shebeiname{
  font-weight: bold;
}
/* 数据 */
.temp_data{
  height: 805px;
}
.temp_datal{
  width: 1500px;
  display: flex;
}
.temp_datar{
  width: 490px;
  margin-left: 20px;
}
.device_border{
  width: 1024px; 
  height: 600px;
  margin-top: 90px;
  /* border: 1px solid #5981a566; */
  border: 1px solid #5981a566;
  box-shadow: 0px 0px 12px #b4e9ff73;
  border-radius: 10px;
}
.device_top{
  width: 100%;
  height: 450px;
}
.device_title{
  width: 300px;
  height: 40px;
  line-height: 40px;
  padding: 10px 0px 10px 30px;
  font-size: 18px
}
.device_datatop{
  width: 100%;
  display: flex;
}
.device_dianya{
  width: 500px;
  height: 320px;
  text-align: center;
  position: relative;
}
.device_dianya img{
  height: 100%;
}
.device_dianya_data{
  position: absolute;
  width: 485px;
  text-align: center;
  top: 145px;
  font-size: 30px;
}
.device_dianya_right{
  padding-top: 45px;
}
.device_data_flex{
  display: flex;
  align-items: self-end;
  margin-bottom: 45px;
}
.device_data_flex img{
  height: 40px;
  margin-right: 30px;
  position: relative;
  top: 3px;
}
.device_data_title{
  font-size: 27px;
  width: 90px;
  margin-right: 20px;
}
.device_data_value{
  width: 140px;
  font-size: 34px;
  color: #5eceff;
  position: relative;
  top: 4px;
}
.device_data_flag{
  font-size: 27px;
  color: #c9e7ff;
}
.device_databottom{
  display: flex;
  padding-top: 45px;
}
.device_databottom_div{
  width: 50%;
}
.device_databottom_flex{
  display: flex;
  align-items: self-end;
  margin-bottom: 45px;
} 
.device_databottom_title{
  width: 260px;
  text-align: right;
  padding-right: 20px;
  font-size: 27px;
}
.device_databottom_value{
  width: 110px;
  font-size: 34px;
  color: #5eceff;
  position: relative;
  top: 4px;
}
.device_databottom_title1{
  width: 170px;
  text-align: right;
  padding-right: 20px;
  font-size: 27px;
}
.device_databottom_value1{
  width: 107px;
  font-size: 34px;
  color: #5eceff;
  position: relative;
  top: 4px;
  overflow: hidden;
}
.device_databottom_flex2{
  height: 45px;
}
.device_databottom_value2{
  width: 107px;
  font-size: 30px;
  color: #5eceff;
  position: relative;
  top: 4px;
  overflow: hidden;
}
.device_databottom_flag{
  font-size: 27px;
  color: #c9e7ff;
}

/* 在线时间 */
.temp_time_mode{
  width: 400px;
  height: 540px;
  /* margin: 0 auto; */
  padding-top: 30px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-top: 120px;
  margin-left: 50px;
}
.temp_peak_name{
  width: 100%;
  font-size: 22px;
  margin-left: 20px;
}
.temp_time{
  margin-top: 80px;
  margin-left: 25px;
  height: 310px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.temp_time_line{
  margin-bottom: 90px;
  position: relative;
  top: 11px;
  display: flex;
  font-size: 20px;
}
.temp_time{
  margin-right: 30px;
  color: #d4d3d3;
}
.temp_time .temp_warning_title{
  width: 110px;
}
.temp_time_year{
  width: 150px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_hour{
  width: 40px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_h{
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
  position: relative;
  top: -5px;
}
.temp_time_s{
  color: #5eceff;
  margin-left: 5px;
}

</style>